package org.pku_uestc.hackathon;

import java.util.Timer;
import java.util.TimerTask;

import org.pku_uestc.hackathon.bean.ISensorHandler;
import org.pku_uestc.hackathon.bean.Sensor;
import org.pku_uestc.hackathon.bean.SensorFactory;
import org.pku_uestc.hackathon.handler.SensorHandlerFactory;
import org.pku_uestc.hackathon.utils.PrefUtils;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.SparseIntArray;

public class CheckService extends Service {
	
	private final IBinder mBinder = new LocalBinder(); 
	
	private SparseIntArray map;    //handler的type是key，sensor的哈希是value
	private SensorHandlerFactory handlerFactory;
	private SensorFactory sensorFactory;
	
	private Timer timer;
	private int minute;
	
	public static final String phone_pre = "phone_";
	public static final String light_pre = "light_";
	
	@Override
	public IBinder onBind(Intent intent) {
		return mBinder;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		initMap();
		minute = 0;
		timer = new Timer();
	    timer.schedule(new MyTimer(), 1000*10, 1000*10);
	}
	
	private void initMap(){
		map = new SparseIntArray();
		map.put(ISensorHandler.TYPE_HIGHHUMHANDLER, Sensor.TYPE_TEMPHUMI+100*11);
		
		handlerFactory = new SensorHandlerFactory(this);
		sensorFactory = new SensorFactory(map.size());
		
		handlerFactory.getSensorHandler(ISensorHandler.TYPE_HIGHHUMHANDLER);
		sensorFactory.getSensor(Sensor.TYPE_TEMPHUMI, 11);
		
		initState(ISensorHandler.TYPE_HIGHHUMHANDLER);
	}

	@Override
	public void onDestroy() {
		timer.cancel();
		super.onDestroy();
	}

	@Override
	public void onStart(Intent intent, int startid) {
		super.onStart(intent, startid);
	}
	
	class MyTimer extends TimerTask{
		
		@Override
	    public void run(){
			minute ++;
			sensorFactory.request(minute);
		}
	}
	
	public void initState(int handlerType){
		if(PrefUtils.getBooleanValue(this.getBaseContext(), phone_pre
				+handlerType, false) 
				||
				!PrefUtils.getStringValue(this.getBaseContext(), light_pre
						+handlerType,"").trim().equals("")){
			sensorFactory.addHandler(map.get(handlerType)%100, 
					map.get(handlerType)/100, handlerFactory.getSensorHandler(handlerType));
		}
	}
	
	public void handlerOn(int handlerType, boolean phone, Integer... lightids){
		sensorFactory.addHandler(map.get(handlerType)%100, 
				map.get(handlerType)/100, handlerFactory.getSensorHandler(handlerType));
		if(phone){
			PrefUtils.putBooleanValue(this.getBaseContext(), phone_pre
					+handlerType, true);
		}else{
			String s = PrefUtils.getStringValue(this.getBaseContext(), light_pre
					+handlerType, "");
			for(Integer i:lightids){
				if(!s.contains(i+""))
					s = s+" "+i;
			}
			PrefUtils.putStringValue(this.getBaseContext(), light_pre
					+handlerType, s);
		}
	}
	
	public void handlerOff(int handlerType, boolean phone, Integer... lightids){
		
		if(phone){
			PrefUtils.putBooleanValue(this.getBaseContext(), phone_pre
					+handlerType, false);
		}else{
			String s = PrefUtils.getStringValue(this.getBaseContext(), light_pre
					+handlerType, "");
			for(Integer i:lightids){
				if(s.contains(i+""))
					s = s.contains(" "+i)?s.replace(" "+i, ""):s.replace(""+i, "");
			}
			PrefUtils.putStringValue(this.getBaseContext(), light_pre
					+handlerType, s);
		}
		
		if(!PrefUtils.getBooleanValue(this.getBaseContext(), phone_pre
					+handlerType, false) 
					&&
					PrefUtils.getStringValue(this.getBaseContext(), light_pre
							+handlerType,"").trim().equals("")){
			sensorFactory.removeHandler(map.get(handlerType)%100, 
					map.get(handlerType)/100, handlerFactory.getSensorHandler(handlerType));
		}
	}
	
	public class LocalBinder extends Binder {  
		CheckService getService() {  
            // 返回Activity所关联的Service对象，这样在Activity里，就可调用Service里的一些公用方法和公用属性  
            return CheckService.this;  
        }  
    }  
}
